

package cn.shengchao.examstar.me.course.ohs.remote.mq;

import cn.shengchao.base.event.PaySuccessMessageEvent;
import cn.shengchao.examstar.me.course.ohs.local.appervices.MyCourseAppService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class OrderPaySuccessEventListener {

    @Autowired
    private MyCourseAppService myCourseAppService;

    @EventListener
    public void handleCustomEvent(PaySuccessMessageEvent event) {
        JSONObject order = JSON.parseObject(JSON.toJSONString(event.getSource()));
        //用户id
        Long userId = order.getLong("userId");
        //产品id
        Long productId = order.getLong("productId");
        //订单号
        String orderId = order.getString("tradeNo");
        //订单号
        String productType = order.getString("productType");
        String payType = order.getString("payType");
        //子产品id
        List<Long> subProductIds = order.getJSONArray("subProductIds")
            .stream()
            .map(item -> Long.valueOf(item.toString()))
            .toList();
        if ("COURSE".equals(productType) || "COURSE_NODES".equals(productType)) {
            log.info("产品处理，收到订单:{},支付完成事件,用户:{},产品:{},子产品:{}", orderId, userId, productId, subProductIds);
            if ( !"0".equals(payType)) {
                myCourseAppService.addCourse(orderId, userId, productId, subProductIds, 1L);
            }else {
                //免费解锁的，api已经添加了
            }
        }
    }
}
